#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

[ -f /etc/conf.d/vdr ] && . /etc/conf.d/vdr

PID=`pidof -o %PPID /usr/bin/vdr`
case "$1" in
  start)
    stat_busy "Starting Video Disk Recorder"
    if [ -z "$PID" ]; then
      if [ "$VDR_IN_SCREEN" == "1" ]; then
        screen -X screen /usr/bin/runvdr || screen -d -m /usr/bin/runvdr
      else
        /usr/bin/runvdr --daemon --silent
      fi
    fi
    if [ $? -gt 0 ]; then
      stat_fail
    else
      PID=`pidof -o %PPID /usr/bin/vdr`
      echo $PID >/var/run/vdr.pid
      add_daemon vdr
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping Video Disk Recorder"
    [ ! -z "$PID" ]  && kill $PID &>/dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon vdr
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 5
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0
